<?php

namespace front\modules\web\controllers;

use users\services\UserService;
use Yii;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backfeed\services\CommentService;
use yii\helpers\ArrayHelper;
use goods\services\ProductService;

/**
 * CommentController implements the CRUD actions for CommentModel model.
 */
class CommentController extends WebBaseController
{
    public $layout = 'main';

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                     'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all CommentModel models.
     * @return mixed
     * 商品评论列表
     * www.wdt.com/comment/index?page=2&per-page=3&sort=+id
     */
    public function actionIndex()
    {
        $data = yii::$app->request->get();
        $data['getByWeb'] = 1;
        $product = ProductService::ProductInfo($data['product_id']);
        $product['product'] = ArrayHelper::toArray($product);
        $product = $product['product']['result']['productInfo']['item_url_map'];
        $comment['list'] =[];
        foreach($product as $key => $item) {
            $item['getByWeb'] = 1;
            $info = CommentService::search($item);
            if($info['code'] == 0) {
                $info['result'] = ArrayHelper::toArray($info['result']);
                if (is_array($info['result'])) {
                    foreach ($info['result'] as $com) {
                        $arr['created_at'] = date('Y-m-d H:i:s', $com['created_at']);
                        $arr['content'] = $com['content'];
                        $arr['product'] = $product[$key];
                        $user = UserService::getUserInfo($com['user_id']);
                        $user = ArrayHelper::toArray($user['result']);
                        $arr['user_name'] = !empty($user['username']) ? $user['username'] : $user['mobile'];
                        array_push($comment['list'], $arr);
                    }
                }
            }
        }
        $created_at = array();
        foreach ($comment['list'] as $item) {
            $created_at[] = $item['created_at'];
        }
        array_multisort($created_at, SORT_DESC, $comment['list']);
        $comment['count'] = count($comment['list']);
        return $this->render('index',['data'=>$comment]);
    }


    /**
     * 插入评论
     */

    public function actionAdd()
    {
        $data['product_id'] = intval(yii::$app->request->get('product_id'));
        if(yii::$app->request->isAjax){
            $data['user_id'] = $this->getUserId();
            $data['created_at'] = time();
            $data['product_id'] = yii::$app->request->get('pid');
            $data['content'] =strip_tags(yii::$app->request->get('content'));
            $result = CommentService::add($data);
            if($result['code']== 0) {
                return 1;
            }
            return $data['product_id'];
        }
           $product = ProductService::ProductInfo($data['product_id']);
           $product = ArrayHelper::toArray($product['result']);
           $info['product']['attributes'] = $product['productInfo']['item_url_map'];
           foreach($info['product']['attributes'] as $item){
               if($data['product_id'] == $item['product_id']){
                   $info['product']['attributes'] = $item;
                   break;
               }
           }
           $info['product']['thumb'] = static::getImgPath($product['productInfo']['thumb'][0]);
//           $info['product']['thumb'] = 'http://backend.shopbox.com' . $product['productInfo']['thumb'][0];
           $info['product']['name'] = $product['productInfo']['name'];
           $info['product']['price'] = static::price_format($product['productInfo']['price']);
           $info['product_id'] = $data['product_id'];
           return $this->render('add', ['data' => $info]);
    }


}
